/**
 * 
 */
package com.yidu.drug.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugMaterialsModelMapper;
import com.yidu.drug.dao.DrugModelMapper;
import com.yidu.drug.dao.DrugProductionModelMapper;
import com.yidu.drug.dao.DrugTypeModelMapper;
import com.yidu.drug.dao.RepertoryModelMapper;
import com.yidu.drug.dao.SpecificationModelMapper;
import com.yidu.drug.model.DrugManagerModel;
import com.yidu.drug.model.DrugModel;
import com.yidu.drug.model.DrugProductionModel;
import com.yidu.drug.model.DrugTypeModel;
import com.yidu.drug.model.RepertoryModel;
import com.yidu.drug.model.SpecificationModel;
import com.yidu.drug.service.DrugModelService;

/**
 * 类的描述
 * @author xiaodozi
 * @date 2018年8月13日 上午11:38:15 
 * @version 1.0
 */
@Service
public class DrugModelServiceImpl implements DrugModelService{
	
	//药品dao
	@Resource
	private DrugModelMapper drugModelMapperDao;

	//药品类型dao
	@Resource
	private DrugTypeModelMapper drugTypeModelMapperDao;
	
	//库存dao
	@Resource
	private RepertoryModelMapper repDao;
	
	//说明书的DAO
	@Resource
	private SpecificationModelMapper speDao;
	
	//药品配置表DAO
	@Resource
	private DrugMaterialsModelMapper dmDao;
	
	//生产计划DAO
	@Resource
	private DrugProductionModelMapper dpDao;
	/**
	 * 查询药品表的一些要显示在订单详细表的数据
	 * yuanwei
	 */
	@Override
	public Map<String, Object> dindanfindAll(String limit, String page, String drugName,String dtId,String drugBarCode,String drugPinyin,String priceMin,String priceMax) {
		//NEW一个材料的模型,加入页面信息和类型名称
		DrugModel drugModel = new DrugModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		drugModel.setPage(pa);
		drugModel.setDrugName(drugName);
		drugModel.setDtId(dtId);
		drugModel.setDrugBarCode(drugBarCode);
		drugModel.setDrugPinyin(drugPinyin);
		drugModel.setPriceMax(priceMax);
		drugModel.setPriceMin(priceMin);
		List<DrugModel> list=drugModelMapperDao.dindanfindAll(drugModel);
		for (DrugModel drugModel2 : list) {
	        /*
	         * 查询药品类型名称
	         */
	        String typeId=drugModel2.getDtId();
	        if(!Tools.isEmpty(typeId)){
	        	DrugTypeModel druType=drugTypeModelMapperDao.selectByPrimaryKey(typeId);
	  	        System.out.println("药品类型名称"+druType.getDtName());
	  	        drugModel2.setDtName(druType.getDtName());
	        }
	        drugModel2.setDrugImg("../../../"+drugModel2.getDrugImg());
		}
		//查询出的信息的总行数
		Long count = drugModelMapperDao.findRows(drugModel);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}

	/**
	 * 分页，条件查询
	 */
	@Override
	public Map<String, Object> findAll(String limit, String page, String drugName,String dtId,String drugBarCode,String drugPinyin,String priceMin,String priceMax) {
		//NEW一个材料的模型,加入页面信息和类型名称
		DrugModel drugModel = new DrugModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		drugModel.setPage(pa);
		drugModel.setDrugName(drugName);
		drugModel.setDtId(dtId);
		drugModel.setDrugBarCode(drugBarCode);
		drugModel.setDrugPinyin(drugPinyin);
		drugModel.setPriceMax(priceMax);
		drugModel.setPriceMin(priceMin);
		//查询所有信息或者按条件查询
		List<DrugModel> list=drugModelMapperDao.findAll(drugModel);
		for (DrugModel drugModel2 : list) {
	        /*
	         * 查询药品类型名称
	         */
	        String typeId=drugModel2.getDtId();
	        if(!Tools.isEmpty(typeId)){
	        	DrugTypeModel druType=drugTypeModelMapperDao.selectByPrimaryKey(typeId);
	  	        if(druType!=null){
	  	        	System.out.println("药品类型名称"+druType.getDtName());
		  	        drugModel2.setDtName(druType.getDtName());
	  	        }
	        }
	        drugModel2.setDrugImg("../../../"+drugModel2.getDrugImg());
		}
		//查询出的信息的总行数
		Long count = drugModelMapperDao.findRows(drugModel);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}
	 /**
     * 查询库存总所有的药品
     * @param limit 行数
     * @param page 页数
     * @param drugName 名字
     * @param dtId 药品id
     * @param drugBarCode 备注
     * @param drugPinyin 
     * @param priceMin 最小金额
     * @param priceMax 最大金额
     * @return map集合
     */
	@Override
	public Map<String, Object> findFilIdAll(HttpSession session,String limit, String page, String drugName,String dtId,String drugBarCode,String drugPinyin,String priceMin,String priceMax) {
		DrugManagerModel manager = (DrugManagerModel) session.getAttribute("manager");
		//NEW一个材料的模型,加入页面信息和类型名称
		DrugModel drugModel = new DrugModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		drugModel.setPage(pa);
		drugModel.setDrugName(drugName);
		drugModel.setDtId(dtId);
		drugModel.setDrugBarCode(drugBarCode);
		drugModel.setDrugPinyin(drugPinyin);
		drugModel.setPriceMax(priceMax);
		drugModel.setPriceMin(priceMin);
		drugModel.setFilId(manager.getFilId());
		//查询所有信息或者按条件查询
		List<DrugModel> list=drugModelMapperDao.findFilIdAll(drugModel);
		for (DrugModel drugModel2 : list) {
	        /*
	         * 查询药品类型名称
	         */
	        String typeId=drugModel2.getDtId();
	        if(!Tools.isEmpty(typeId)){
	        	DrugTypeModel druType=drugTypeModelMapperDao.selectByPrimaryKey(typeId);
	  	        System.out.println("药品类型名称"+druType.getDtName());
	  	        drugModel2.setDtName(druType.getDtName());
	        }
	        drugModel2.setDrugImg("../../../"+drugModel2.getDrugImg());
		}
		//查询出的信息的总行数
		Long count = drugModelMapperDao.findFilIdRows(drugModel);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		return map;
	}
	/**
	 * 根据药品ID删除
	 */
	@Override
	public int deleteByPrimaryKey(String drugId) {
		DrugProductionModel dp=new DrugProductionModel();
		dp.setDrugId(drugId);
		Long ro=dpDao.findRows(dp);
		if(ro<0){
			Long num=0L;
			int count=repDao.deleteByPwId(drugId,num);
			int rows=0;
			if(count>0){
				speDao.deleteByDrugId(drugId);
				dmDao.deleteByDrugId(drugId);
				rows=drugModelMapperDao.deleteByPrimaryKey(drugId);
				return rows;
			}
			return count;
		}else{
			return -1;
		}
	}
	/**
	 * 增加所有药品信息
	 */
	@Override
	public int insert(DrugModel record) {
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 增加部分药品信息
	 */
	@Override
	public int insertSelective(DrugModel record) {
		//药品ID
		String id=Tools.getRandomUUID();
		record.setDrugId(id);
		//药品条形码
		Random random = new Random();
		String result="";
		for (int i=0;i<10;i++)
		{
			result+=random.nextInt(10);
		}
		System.out.println(result);
		record.setDrugBarCode(result);
		//获取当前日期时间
		Timestamp ss = new Timestamp(new Date().getTime());
		record.setCztime(ss);
		//是否有效
		record.setIsva(Tools.ISVA_YES);
		//添加
		int count=drugModelMapperDao.insertSelective(record);
		if(count>0){
			/*
			 * 增加仓库信息
			 */
			RepertoryModel rep=new RepertoryModel();
			String repId=Tools.getRandomUUID();
			rep.setRepId(repId);
			rep.setOptime(ss);
			rep.setRepNumber(0L);
			rep.setIsva(Tools.ISVA_YES);
			rep.setFilId("1");
			rep.setPwId(id);
			repDao.insertSelective(rep);
			
			/*
			 * 增加说明书信息
			 */
			SpecificationModel spe=new SpecificationModel();
			String speId=Tools.getRandomUUID();
			spe.setSpeId(speId);
			spe.setDrugId(id);
			spe.setSpeName(record.getDrugName()+"说明书");
			speDao.insertSelective(spe);
		}
		return count;
	}

	@Override
	public DrugModel selectByPrimaryKey(String drugId) {
		DrugModel drug=drugModelMapperDao.selectByPrimaryKey(drugId);
		return drug;
	}

	@Override
	public int updateByPrimaryKeySelective(DrugModel record) {
		///获取当前日期时间
		Timestamp ss = new Timestamp(new Date().getTime());
		record.setCztime(ss);
		int count=drugModelMapperDao.updateByPrimaryKeySelective(record);
		if(count>0){
			String speName=record.getDrugName()+"说明书";
			String drugId=record.getDrugId();
			speDao.updateByDrugId(speName,drugId);
			return count;
		}
		return count;
	}

	@Override
	public int updateByPrimaryKey(DrugModel record) {
		// TODO Auto-generated method stub
		return 0;
	}

}
